<template>
  <div id="login">
    <el-form :rules="loginRules" ref="loginForm" label-width="80px" :model="user" v-show="loginVisible">
      <el-form-item label="用户名" prop="user">
        <el-input v-model="user.user"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="psw">
        <el-input type="password" v-model="user.psw"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onLogin">登录</el-button>
        <!-- <el-button @click="toUpdatePsw">修改密码</el-button> -->
      </el-form-item>
    </el-form>
    <el-form :rules="updateRules" ref="updateForm" label-width="80px" :model="update" v-show="updatePswVisible">
      <el-form-item label="用户名" prop="user">
        <el-input v-model="update.user"></el-input>
      </el-form-item>
      <el-form-item label="原密码" prop="psw">
        <el-input type="password" v-model="update.psw"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPsw">
        <el-input type="password" v-model="update.newPsw"></el-input>
      </el-form-item>
      <el-form-item label="再次输入" prop="againPsw">
        <el-input type="password" v-model="update.againPsw"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updatePsw">修改</el-button>
        <el-button @click="toLogin">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script type="text/javascript">
export default {
  data () {
    return {
      user: {
        user: '',
        psw: ''
      },
      update: {
        user: '',
        psw: '',
        newPsw: '',
        againPsw: ''
      },
      loginRules: {
        user: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        psw: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      updateRules: {
        user: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        psw: [
          { required: true, message: '请输入原密码', trigger: 'blur' }
        ],
        newPsw: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        againPsw: [
          { required: true, message: '请输再次输入新密码', trigger: 'blur' }
        ]
      },
      updatePswVisible: false,
      loginVisible: true
    }
  },
  methods: {
    onLogin () {
      if (this.user.user === 'admin' && this.user.psw === 'Admin112358') {
        this.$emit('admin')
      }else{
        this.$message.error('用户名或密码错误！')
      }

    },
    toUpdatePsw () {
      this.updatePswVisible = true
      this.loginVisible = false
    },
    toLogin () {
      this.updatePswVisible = false
      this.loginVisible = true
    },
    updatePsw () {
      this.$refs['updateForm'].validate((valid) => {
        if (valid) {
          if (this.update.newPsw !== this.update.againPsw) {
            this.$alert('两次输入的密码不一致')
          } else {
            this.$api.call({
              service: 'update-psw',
              body: this.update
            }).then((data) => {
              return this.$alert('密码修改成功!')
            }).then(() => {
              this.toLogin()
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style type="text/css">
  #login {
    width: 50%;
    height: 200px;
    margin: 200px auto;
  }
</style>